home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / Firefox Setup 3.0.6.exe / nonlocalized / chrome / browser.jar / content / browser / sanitize.js < prev    next >
Encoding:
JavaScript  |  2008-01-22  |  9.3 KB  |  298 lines

  1. //@line 39 "/e/fx19rel/WINNT_5.2_Depend/mozilla/browser/base/content/sanitize.js"
  2.  
  3. function Sanitizer() {}
  4. Sanitizer.prototype = {
  5.   // warning to the caller: this one may raise an exception (e.g. bug #265028)
  6.   clearItem: function (aItemName)
  7.   {
  8.     if (this.items[aItemName].canClear)
  9.       this.items[aItemName].clear();
  10.   },
  11.  
  12.   canClearItem: function (aItemName)
  13.   {
  14.     return this.items[aItemName].canClear;
  15.   },
  16.   
  17.   _prefDomain: "privacy.item.",
  18.   getNameFromPreference: function (aPreferenceName)
  19.   {
  20.     return aPreferenceName.substr(this._prefDomain.length);
  21.   },
  22.   
  23.   /**
  24.    * Deletes privacy sensitive data in a batch, according to user preferences
  25.    *
  26.    * @returns  null if everything's fine;  an object in the form
  27.    *           { itemName: error, ... } on (partial) failure
  28.    */
  29.   sanitize: function ()
  30.   {
  31.     var psvc = Components.classes["@mozilla.org/preferences-service;1"]
  32.                          .getService(Components.interfaces.nsIPrefService);
  33.     var branch = psvc.getBranch(this._prefDomain);
  34.     var errors = null;
  35.     for (var itemName in this.items) {
  36.       var item = this.items[itemName];
  37.       if ("clear" in item && item.canClear && branch.getBoolPref(itemName)) {
  38.         // Some of these clear() may raise exceptions (see bug #265028)
  39.         // to sanitize as much as possible, we catch and store them, 
  40.         // rather than fail fast.
  41.         // Callers should check returned errors and give user feedback
  42.         // about items that could not be sanitized
  43.         try {
  44.           item.clear();
  45.         } catch(er) {
  46.           if (!errors) 
  47.             errors = {};
  48.           errors[itemName] = er;
  49.           dump("Error sanitizing " + itemName + ": " + er + "\n");
  50.         }
  51.       }
  52.     }
  53.     return errors;
  54.   },
  55.   
  56.   items: {
  57.     cache: {
  58.       clear: function ()
  59.       {
  60.         const cc = Components.classes;
  61.         const ci = Components.interfaces;
  62.         var cacheService = cc["@mozilla.org/network/cache-service;1"]
  63.                              .getService(ci.nsICacheService);
  64.         try {
  65.           cacheService.evictEntries(ci.nsICache.STORE_ANYWHERE);
  66.         } catch(er) {}
  67.       },
  68.       
  69.       get canClear()
  70.       {
  71.         return true;
  72.       }
  73.     },
  74.     
  75.     cookies: {
  76.       clear: function ()
  77.       {
  78.         var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"]
  79.                                   .getService(Components.interfaces.nsICookieManager);
  80.         cookieMgr.removeAll();
  81.       },
  82.       
  83.       get canClear()
  84.       {
  85.         return true;
  86.       }
  87.     },
  88.     
  89.     offlineApps: {
  90.       clear: function ()
  91.       {
  92.         const Cc = Components.classes;
  93.         const Ci = Components.interfaces;
  94.         var cacheService = Cc["@mozilla.org/network/cache-service;1"].
  95.                            getService(Ci.nsICacheService);
  96.         try {
  97.           cacheService.evictEntries(Ci.nsICache.STORE_OFFLINE);
  98.         } catch(er) {}
  99.  
  100.         var storageManagerService = Cc["@mozilla.org/dom/storagemanager;1"].
  101.                                     getService(Ci.nsIDOMStorageManager);
  102.         storageManagerService.clearOfflineApps();
  103.       },
  104.  
  105.       get canClear()
  106.       {
  107.           return true;
  108.       }
  109.     },
  110.  
  111.     history: {
  112.       clear: function ()
  113.       {
  114.         var globalHistory = Components.classes["@mozilla.org/browser/global-history;2"]
  115.                                       .getService(Components.interfaces.nsIBrowserHistory);
  116.         globalHistory.removeAllPages();
  117.         
  118.         try {
  119.           var os = Components.classes["@mozilla.org/observer-service;1"]
  120.                              .getService(Components.interfaces.nsIObserverService);
  121.           os.notifyObservers(null, "browser:purge-session-history", "");
  122.         }
  123.         catch (e) { }
  124.         
  125.         // Clear last URL of the Open Web Location dialog
  126.         var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  127.                               .getService(Components.interfaces.nsIPrefBranch2);
  128.         try {
  129.           prefs.clearUserPref("general.open_location.last_url");
  130.         }
  131.         catch (e) { }
  132.       },
  133.       
  134.       get canClear()
  135.       {
  136.         // bug 347231: Always allow clearing history due to dependencies on
  137.         // the browser:purge-session-history notification. (like error console)
  138.         return true;
  139.       }
  140.     },
  141.     
  142.     formdata: {
  143.       clear: function ()
  144.       {
  145.         //Clear undo history of all searchBars
  146.         var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  147.         var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
  148.         var windows = windowManagerInterface.getEnumerator("navigator:browser");
  149.         while (windows.hasMoreElements()) {
  150.           var searchBar = windows.getNext().document.getElementById("searchbar");
  151.           if (searchBar) {
  152.             searchBar.value = "";
  153.             searchBar.textbox.editor.transactionManager.clear();
  154.           }
  155.         }
  156.  
  157.         var formHistory = Components.classes["@mozilla.org/satchel/form-history;1"]
  158.                                     .getService(Components.interfaces.nsIFormHistory2);
  159.         formHistory.removeAllEntries();
  160.       },
  161.       
  162.       get canClear()
  163.       {
  164.         var formHistory = Components.classes["@mozilla.org/satchel/form-history;1"]
  165.                                     .getService(Components.interfaces.nsIFormHistory2);
  166.         return formHistory.hasEntries;
  167.       }
  168.     },
  169.     
  170.     downloads: {
  171.       clear: function ()
  172.       {
  173.         var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
  174.                               .getService(Components.interfaces.nsIDownloadManager);
  175.         dlMgr.cleanUp();
  176.       },
  177.  
  178.       get canClear()
  179.       {
  180.         var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
  181.                               .getService(Components.interfaces.nsIDownloadManager);
  182.         return dlMgr.canCleanUp;
  183.       }
  184.     },
  185.     
  186.     passwords: {
  187.       clear: function ()
  188.       {
  189.         var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
  190.                               .getService(Components.interfaces.nsILoginManager);
  191.         pwmgr.removeAllLogins();
  192.       },
  193.       
  194.       get canClear()
  195.       {
  196.         var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
  197.                               .getService(Components.interfaces.nsILoginManager);
  198.         var count = pwmgr.countLogins("", "", ""); // count all logins
  199.         return (count > 0);
  200.       }
  201.     },
  202.     
  203.     sessions: {
  204.       clear: function ()
  205.       {
  206.         // clear all auth tokens
  207.         var sdr = Components.classes["@mozilla.org/security/sdr;1"]
  208.                             .getService(Components.interfaces.nsISecretDecoderRing);
  209.         sdr.logoutAndTeardown();
  210.  
  211.         // clear plain HTTP auth sessions
  212.         var authMgr = Components.classes['@mozilla.org/network/http-auth-manager;1']
  213.                                 .getService(Components.interfaces.nsIHttpAuthManager);
  214.         authMgr.clearAll();
  215.       },
  216.       
  217.       get canClear()
  218.       {
  219.         return true;
  220.       }
  221.     }
  222.   }
  223. };
  224.  
  225.  
  226.  
  227. // "Static" members
  228. Sanitizer.prefDomain          = "privacy.sanitize.";
  229. Sanitizer.prefPrompt          = "promptOnSanitize";
  230. Sanitizer.prefShutdown        = "sanitizeOnShutdown";
  231. Sanitizer.prefDidShutdown     = "didShutdownSanitize";
  232.  
  233. Sanitizer._prefs = null;
  234. Sanitizer.__defineGetter__("prefs", function() 
  235. {
  236.   return Sanitizer._prefs ? Sanitizer._prefs
  237.     : Sanitizer._prefs = Components.classes["@mozilla.org/preferences-service;1"]
  238.                          .getService(Components.interfaces.nsIPrefService)
  239.                          .getBranch(Sanitizer.prefDomain);
  240. });
  241.  
  242. // Shows sanitization UI
  243. Sanitizer.showUI = function(aParentWindow) 
  244. {
  245.   var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  246.                      .getService(Components.interfaces.nsIWindowWatcher);
  247. //@line 287 "/e/fx19rel/WINNT_5.2_Depend/mozilla/browser/base/content/sanitize.js"
  248.   ww.openWindow(aParentWindow,
  249. //@line 289 "/e/fx19rel/WINNT_5.2_Depend/mozilla/browser/base/content/sanitize.js"
  250.                 "chrome://browser/content/sanitize.xul",
  251.                 "Sanitize",
  252.                 "chrome,titlebar,centerscreen,modal",
  253.                 null);
  254. };
  255.  
  256. /** 
  257.  * Deletes privacy sensitive data in a batch, optionally showing the 
  258.  * sanitize UI, according to user preferences
  259.  *
  260.  * @returns  null if everything's fine (no error or displayed UI,  which
  261.  *           should handle errors);  
  262.  *           an object in the form { itemName: error, ... } on (partial) failure
  263.  */
  264. Sanitizer.sanitize = function(aParentWindow) 
  265. {
  266.   if (Sanitizer.prefs.getBoolPref(Sanitizer.prefPrompt)) {
  267.     Sanitizer.showUI(aParentWindow);
  268.     return null;
  269.   }
  270.   return new Sanitizer().sanitize();
  271. };
  272.  
  273. Sanitizer.onStartup = function() 
  274. {
  275.   // we check for unclean exit with pending sanitization
  276.   Sanitizer._checkAndSanitize();
  277. };
  278.  
  279. Sanitizer.onShutdown = function() 
  280. {
  281.   // we check if sanitization is needed and perform it
  282.   Sanitizer._checkAndSanitize();
  283. };
  284.  
  285. // this is called on startup and shutdown, to perform pending sanitizations
  286. Sanitizer._checkAndSanitize = function() 
  287. {
  288.   const prefs = Sanitizer.prefs;
  289.   if (prefs.getBoolPref(Sanitizer.prefShutdown) && 
  290.       !prefs.prefHasUserValue(Sanitizer.prefDidShutdown)) {
  291.     // this is a shutdown or a startup after an unclean exit
  292.     Sanitizer.sanitize(null) || // sanitize() returns null on full success
  293.       prefs.setBoolPref(Sanitizer.prefDidShutdown, true);
  294.   }
  295. };
  296.  
  297.  
  298.